
$(function() {
   //查询
	$("#searchAgent").on("click",function(){
		//  $("#agent_content input").val("");
		var reg = /^\d+$/; //正整数
		var agentinfo =  $("#agentinfo").val();
	  if(!agentinfo){
        	$('.help-block').html("请输入手机号")
    		$(' .help-block').css('color','red'); 
        	return;
        }else{
        	$('.help-block').html("")
        }
	  
		$("#agent_content").removeClass("hidden");
		$("#rechargeAgent").removeClass("hidden");
	window.location.href="/p/biz/acs/getAgent?ap="+agentinfo;
	});
	
	  // 充值
    $('#rechargeAgent').click(function(event) {
        var code = $("input[name='id']").val();
    	var agentinfo =  $("#agentinfo").val();
  	  if(!agentinfo){
          	$('.help-block').html("请输入手机号")
      		$(' .help-block').css('color','red'); 
          	return;
          }else{
          	$('.help-block').html("")
          }
        if(!code){
        	$('.help-block').html("查询不到数据，请输入正确的手机号再查询")
    		$(' .help-block').css('color','red'); 
        	return;
        }else{
        	$('.help-block').html("")
        }
        var url= "/a/biz/agent/getCount";      
        $.ajax({
            type: "POST",
            dataType: "json",
            async:false, 
            url:url,
            data: {"code":code},
            success: function (result) {
            	if(result.status=='2'){
            		$('.help-block').html(result.message);
            		$(' .help-block').css('color','red'); 
                	return;
            	}
            	if(result.data.notCount == 0){
            		$('.help-block').html("上级套数不足,暂不能添加！");
            		$(' .help-block').css('color','red'); 
            		return;
            	}
            	$('#modal_new_role').modal('show');
            	 if(result.data.notCount == 2){
            		$("#inputCount").attr("placeholder","您可充值套数的范围是:"+result.data.MinCount+"以上");
            		$("input[name='upAgentId']").val(result.data.upAgentId);
            		$("#minCount").val(result.data.MinCount);
                	$("#maxCount").val('999999999999999999');

            	}else{
            	$("#inputCount").attr("placeholder","您可充值套数的范围是:"+result.data.MinCount+"到"+result.data.LevelCode);
            	$("input[name='upAgentId']").val(result.data.upAgentId);
            	$("#minCount").val(result.data.MinCount);
            	$("#maxCount").val(result.data.LevelCode);

            	}
            }
        });
        
    });
    
  $("button[id='sub']").on("click",function(){
	   var code = $("input[name='id']").val();
	   var count = $("#inputCount").val();
	   var minCount= $("#minCount").val();
   	   var maxCount=$("#maxCount").val();
   	   var leftCount=$(".leftCount").val();
	   if(!count){
			$('.help-block1').html("请输入充值套数")
      		$(' .help-block1').css('color','red'); 
          	return;
	   }
  if(agentType=="总部"){
	   if(Number(count)<0){
		  var count1=Math.abs(count); 
		  if(count1>Number(leftCount)){
			  $('.help-block1').html("请输入正确的充值套数")
	      	  $(' .help-block1').css('color','red'); 
	          	return;  
		  }
	   }else if(Number(count)<Number(minCount)){
			$('.help-block1').html("充值套数不能小于"+minCount)
     		$(' .help-block1').css('color','red'); 
         	return;
	   }     
	   }else{
		   if(Number(count)<0){
				$('.help-block1').html("充值套数要大于0")
	     		$(' .help-block1').css('color','red'); 
	         	return;
		   }else if(Number(count)<Number(minCount)){
				$('.help-block1').html("充值套数不能小于"+minCount)
	     		$(' .help-block1').css('color','red'); 
	         	return;
		   }  
	   }
	   if(count=='0'){
			$('.help-block1').html("充值套数不能为0")
     		$(' .help-block1').css('color','red'); 
         	return;
	   } else if(Number(count)>Number(maxCount)){
			$('.help-block1').html("充值套数不能大于"+maxCount)
     		$(' .help-block1').css('color','red'); 
         	return;
	   }
		var upId = $("input[name='upAgentId']").val();
	  var url= "/a/biz/agent/addCount";      
	  $.ajax({
          type: "POST",
          dataType: "json",
          async:false, 
          url:url,
          data: {"code":code,"count":count,"upId":upId},
          success: function (result) {
        	  if (result.status === '1') {
  				// 正确
  	        	window.location.reload();

  			} else if (result.status === '2') {
//  				错误
  				mdTool.showMessage(data.message);
  			}
          }
      });
	  
  })
  
 
	
});

var storage = window.localStorage;  
var localAgent = JSON.parse(storage["agent"]);   
var agentType="";
if(localAgent){
	agentType=localAgent.type;
}
